package MEET.src.View;

import MEET.src.code.User;
import MEET.src.code.UserDao;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;

public class EditUserInfoFrame extends JFrame {
    private User user;
    private JTextField nameField;
    private JTextField ageField;
    private JComboBox<String> genderComboBox;
    private JComboBox<String> areaComboBox;
    private JComboBox<String> identityComboBox;
    private JComboBox<String> deptComboBox;
    private JPanel contentPane;

    public EditUserInfoFrame(User user) {
        this.user = user;
        initComponents();
    }

    private void initComponents() {
        setTitle("上海电力大学会议预订系统 - 修改个人信息");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setSize(550, 550);
        setLocationRelativeTo(null);

        // 主面板设置
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 5));
        setContentPane(contentPane);

        // 1. 顶部标题栏
        createHeaderPanel();

        // 2. 内容区
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BorderLayout());
        mainPanel.setBorder(new EmptyBorder(15, 15, 15, 15));
        mainPanel.setBackground(new Color(240, 245, 249));

        // 内部标题
        JPanel innerTitlePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        innerTitlePanel.setBackground(new Color(240, 245, 249));
        JLabel innerTitle = new JLabel("修改个人信息");
        innerTitle.setFont(new Font("微软雅黑", Font.BOLD, 18));
        innerTitle.setForeground(new Color(0, 125, 153));
        innerTitlePanel.add(innerTitle);
        mainPanel.add(innerTitlePanel, BorderLayout.NORTH);

        // 表单面板
        JPanel formPanel = new JPanel();
        formPanel.setLayout(new GridLayout(7, 2, 15, 15));
        formPanel.setBorder(new EmptyBorder(20, 50, 20, 50));
        formPanel.setBackground(Color.WHITE);

        // 修正边框设置 - 使用复合边框
        formPanel.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(new Color(200, 200, 200)),
                new EmptyBorder(15, 15, 15, 15)
        ));

        Font labelFont = new Font("微软雅黑", Font.BOLD, 14);
        Font fieldFont = new Font("微软雅黑", Font.PLAIN, 14);

        // 姓名
        addFormField(formPanel, "姓名:", labelFont, nameField = new JTextField(user.getName(), 20), fieldFont);
        // 年龄
        addFormField(formPanel, "年龄:", labelFont, ageField = new JTextField(String.valueOf(user.getAge()), 20), fieldFont);
        // 性别
        genderComboBox = new JComboBox<>(new String[]{"男", "女"});
        genderComboBox.setSelectedItem(user.getSex());
        addFormField(formPanel, "性别:", labelFont, genderComboBox, fieldFont);
        // 区域
        areaComboBox = new JComboBox<>(new String[]{"杨浦", "临港"});
        areaComboBox.setSelectedItem(user.getUarea());
        addFormField(formPanel, "区域:", labelFont, areaComboBox, fieldFont);
        // 身份
        identityComboBox = new JComboBox<>(new String[]{"老师", "学生"});
        identityComboBox.setSelectedItem(user.getIdentity());
        addFormField(formPanel, "身份:", labelFont, identityComboBox, fieldFont);
        // 学院
        deptComboBox = new JComboBox<>(new String[]{
                "经济与管理学院", "计算机科学与技术学院", "电子与信息工程学院",
                "电气工程学院", "外国语学院", "环境与化学工程学院",
                "数理学院", "自动化工程学院", "能源与机械工程学院"
        });
        deptComboBox.setSelectedItem(user.getSdept());
        addFormField(formPanel, "学院:", labelFont, deptComboBox, fieldFont);

        mainPanel.add(formPanel, BorderLayout.CENTER);

        // 按钮面板
        JPanel buttonPanel = new JPanel();
        buttonPanel.setBorder(new EmptyBorder(10, 50, 20, 50));
        buttonPanel.setBackground(new Color(240, 245, 249));
        buttonPanel.setLayout(new GridLayout(1, 2, 30, 0));

        // 保存按钮
        JButton saveButton = createStyledButton("保存修改", new Color(70, 130, 180));
        saveButton.addActionListener(e -> saveChanges());
        buttonPanel.add(saveButton);

        // 取消按钮
        JButton cancelButton = createStyledButton("取消", new Color(120, 120, 120));
        cancelButton.addActionListener(e -> this.dispose());
        buttonPanel.add(cancelButton);

        mainPanel.add(buttonPanel, BorderLayout.SOUTH);
        contentPane.add(mainPanel, BorderLayout.CENTER);
    }

    private void createHeaderPanel() {
        JPanel headerPanel = new JPanel();
        headerPanel.setBackground(new Color(0, 125, 153));
        headerPanel.setPreferredSize(new Dimension(100, 50));

        JLabel lblTitle = new JLabel("上海电力大学会议预订系统");
        lblTitle.setForeground(Color.WHITE);
        lblTitle.setFont(new Font("微软雅黑", Font.BOLD, 20));

        headerPanel.add(lblTitle);
        contentPane.add(headerPanel, BorderLayout.NORTH);
    }

    private void addFormField(JPanel panel, String labelText, Font labelFont, JComponent field, Font fieldFont) {
        JLabel label = new JLabel(labelText);
        label.setFont(labelFont);
        label.setHorizontalAlignment(SwingConstants.RIGHT);
        panel.add(label);

        field.setFont(fieldFont);
        if (field instanceof JTextField) {
            ((JTextField) field).setBorder(BorderFactory.createCompoundBorder(
                    BorderFactory.createLineBorder(new Color(180, 180, 180)),
                    new EmptyBorder(5, 10, 5, 10)));
        } else if (field instanceof JComboBox) {
            ((JComboBox<?>) field).setRenderer(new DefaultListCellRenderer() {
                @Override
                public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                    super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                    setFont(fieldFont);
                    setBorder(new EmptyBorder(5, 10, 5, 10));
                    return this;
                }
            });
        }
        panel.add(field);
    }

    private JButton createStyledButton(String text, Color bgColor) {
        JButton button = new JButton(text);
        button.setFont(new Font("微软雅黑", Font.BOLD, 14));
        button.setBackground(bgColor);
        button.setForeground(Color.WHITE);
        button.setFocusPainted(false);
        button.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(bgColor.darker(), 1),
                BorderFactory.createEmptyBorder(8, 20, 8, 20)
        ));
        button.setCursor(new Cursor(Cursor.HAND_CURSOR));

        // 添加悬停效果
        button.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                button.setBackground(bgColor.brighter());
            }

            public void mouseExited(java.awt.event.MouseEvent evt) {
                button.setBackground(bgColor);
            }
        });

        return button;
    }

    private void saveChanges() {
        // 收集表单数据
        String newName = nameField.getText();
        int newAge;
        try {
            newAge = Integer.parseInt(ageField.getText());
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(this, "年龄必须为数字", "输入错误", JOptionPane.ERROR_MESSAGE);
            return;
        }
        String newGender = (String) genderComboBox.getSelectedItem();
        String newArea = (String) areaComboBox.getSelectedItem();
        String newIdentity = (String) identityComboBox.getSelectedItem();
        String newDept = (String) deptComboBox.getSelectedItem();

        // 更新用户对象
        user.setName(newName);
        user.setAge(newAge);
        user.setSex(newGender);
        user.setUarea(newArea);
        user.setIdentity(newIdentity);
        user.setSdept(newDept);

        // 更新数据库
        if (UserDao.updateUser(user)) {
            JOptionPane.showMessageDialog(this, "信息更新成功", "成功", JOptionPane.INFORMATION_MESSAGE);
            this.dispose();
            new UserInfoFrame(user).setVisible(true);
        } else {
            JOptionPane.showMessageDialog(this, "信息更新失败", "错误", JOptionPane.ERROR_MESSAGE);
        }
    }
}